package com.net.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.net.dao.CommodityDao;
import com.net.dao.ProviderDao;
import com.net.dao.SuperDao;
import com.net.model.Commodity;
import com.net.model.User;
@Component("commodityDao")
public class CommodityDaoImpl extends SuperDao implements CommodityDao{
	private ProviderDao providerDao;
	public boolean delete(Commodity u) {
		Commodity cust = (Commodity) this.getHibernateTemplate().load(Commodity.class, u.getId());
		if(cust!=null){
			cust.setCategory(null);
			cust.setProviders(null);
			this.getHibernateTemplate().delete(cust);
			return true;
		}else
			return false;
	}

	@SuppressWarnings("unchecked")
	public List<Commodity> getCommodities() {
		return (List<Commodity>)this.getHibernateTemplate().find("from Commodity");
	}

	public Commodity loadCommodityById(int id) {
		return (Commodity) this.getHibernateTemplate().load(Commodity.class, id);
	}

	@SuppressWarnings("unchecked")
	public Commodity loadCommodityByName(String name) {
		List<Commodity> commodities = getHibernateTemplate().find("from Commodity c where c.commodity_name='"+name+"'");
		if(commodities!=null&&commodities.size()>0){
			return commodities.get(0);
		}else
			return null;
	}

	public void save(Commodity u) {
		this.getHibernateTemplate().save(u);
	}

	public void update(Commodity commodity) {
		System.out.println(commodity.getCurrent_price()+":update price");
		this.getHibernateTemplate().saveOrUpdate(commodity);
	}

	public List<Commodity> searchList(Commodity commodity) {
		System.out.println(commodity.getId()+":"+commodity.getCommodity_name()+":"+commodity.getCategory().getId());
		DetachedCriteria dc = DetachedCriteria.forClass(Commodity.class);
		if(commodity.getId()!=0||!commodity.getCommodity_name().equals("")){
			dc = dc.add(Restrictions.or(Restrictions.eq("id", commodity.getId()), 
					Restrictions.like("commodity_name", commodity.getCommodity_name())));
			if(commodity.getCategory().getId()!=0){
					dc = dc.createAlias("category","ca").add(Restrictions.eq("ca.id", commodity.getCategory().getId()));
			}
			return this.getHibernateTemplate().findByCriteria(dc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY));
		}
		if(commodity.getCategory().getId()!=0){
				System.out.println("^^^^^^^^^^^^^^^^^666666666"+commodity.getCategory().getId());
				dc = dc.createCriteria("category").add(Restrictions.eq("id", commodity.getCategory().getId()));
			return this.getHibernateTemplate().findByCriteria(dc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY));
		}
		return this.getHibernateTemplate().findByCriteria(dc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY));
	}

	public ProviderDao getProviderDao() {
		return providerDao;
	}
	@Resource(name="providerDao")
	public void setProviderDao(ProviderDao providerDao) {
		this.providerDao = providerDao;
	}

	public List<Commodity> getCommodities(final int currentPage, final int perPage, final String orderType ,final String orderName) {
		System.out.println("99999999999");
		HibernateCallback hc = new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				if(orderType==null||orderName==null){
				return session.createQuery("from Commodity").setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}else{
					System.out.println("from Commodity order by "+orderName+" "+ orderType);
					return session.createQuery("from Commodity order by "+orderName+" "+ orderType).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}
			}
		};
		
		System.out.println(getHibernateTemplate().executeFind(hc));
		return (List<Commodity>)getHibernateTemplate().execute(hc);
	}

	public List<Commodity> getCommoditiesByCondition(final int currentPage, final int perPage,
			final String condition) {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String str = "from Commodity c where " + condition;
				System.out.println(str);
				return session.createQuery(str).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc);
	}

	public int getCount() {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from Commodity").list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc).size();
	}

}
